﻿using Commands.Settings;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;

namespace PluginSendToKindle
{
    internal class SettingsHelper 
    {
        internal static IKindlePluginSettings Settings
        {
            get
            {
                if (_settings == null)
                    _settings = _serializer.LoadSettings(_GetFilePath()) as IKindlePluginSettings;

                return _settings;
            }

            set
            {
                _settings = value;
            }
        }

        public static void SaveSettings()
        {
            // TESTCODE
            var settings = new KindlePluginSettings();
            SettingsHelper.Settings = settings;
            settings.Fb2Kf8ConverterPath = @"C:\MyProjects\KindleD\fb2kf8_0.4.5.4\fb2kf8.exe";
            settings.DefaultKindleName = "My";
            settings.Kindles = new List<KindleCredential>();
            settings.Kindles.Add(new KindleCredential { Address = "asasdas@'asdasd.ru", Name = "My" });
            settings.Kindles.Add(new KindleCredential { Address = "asasdas@'asdasd.ru", Name = "my1" });

            _serializer.SaveSettings(_GetFilePath(), settings);
        }

        private static string _GetFilePath()
        {
            string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var settingsFileName = Assembly.GetExecutingAssembly().FullName;

            return Path.Combine(assemblyFolder, SETTINGS_FILE_NAME);
        }

        private static readonly SettingsSerializer<KindlePluginSettings> _serializer = new SettingsSerializer<KindlePluginSettings>(); 

        private static IKindlePluginSettings _settings;

        private const string SETTINGS_FILE_NAME = @"kindle_plugin_settings.xml";
    }

    public interface IKindlePluginSettings
    {
        string Fb2Kf8ConverterPath { get;}

        List<KindleCredential> Kindles { get; }

        string DefaultKindleName { get; }

        string GetDefaultKindleMail();
    }

    public class KindleCredential
    {
        public string Name {get; set;}

        public string Address {get; set;}
    }

    [DataContract]
    [KnownType(typeof(List<KindleCredential>))]
    public class KindlePluginSettings : IKindlePluginSettings
    {

        [DataMember]
        public string Fb2Kf8ConverterPath
        {
            get;
            set;
        }

        [DataMember]
        public List<KindleCredential> Kindles
        {
            get;
            set;
        }

        [DataMember]
        public string DefaultKindleName
        {
            get;
            set;
        }

        public string GetDefaultKindleMail()
        {
            var defaultKindle = Kindles.FirstOrDefault(x => x.Name == DefaultKindleName);

            return defaultKindle != null ? defaultKindle.Address : null;
        }
    }
}
